//
//  SYNewsShowController.m
//  MicroNews
//
//  Created by tongcheng on 15/10/21.
//  Copyright © 2015年 siyue. All rights reserved.
//

#import "SYNewsShowController.h"
#import "SYHttpTool.h"
#import "SYNewsModel.h"
#import "SYNewsFrameModel.h"
#import "SYNewsCell.h"
#import "SYDetailContentController.h"

@interface SYNewsShowController ()

@property(nonatomic,strong) NSMutableArray *arrayList;

@property(nonatomic,assign)BOOL update;

//记载用户下拉刷新 请求新数据的次数
@property (nonatomic,assign) int networkCount;

@end

@implementation SYNewsShowController

-(NSMutableArray *)arrayList
{
    if(_arrayList==nil){
        _arrayList= [NSMutableArray array];
    }
    return _arrayList;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    self.update = YES;
    [self.tableView addHeaderWithTarget:self action:@selector(loadData)];
    [self.tableView addFooterWithTarget:self action:@selector(loaMoreData)];
}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:NO animated:YES];
    //开始刷新
    if (self.update == YES) {
        [self.tableView headerBeginRefreshing];
        self.update = NO;
    }
}

- (void)setUrlString:(NSString *)urlString
{
    _urlString = urlString;
}

#pragma mark - 数据源方法

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrayList.count;

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SYNewsFrameModel *frameModel=self.arrayList[indexPath.row];
    
    
    SYNewsCell *cell=[SYNewsCell cellWithTableView:tableView indextifier:@"newshow"];
    cell.frameModel=frameModel;
    
    return cell;
}

#pragma mark 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SYNewsFrameModel *frameModel=self.arrayList[indexPath.row];
    return frameModel.cellH;
}

/** 预估行高，这个方法可以减少上面方法的调用次数，提高性能 */
- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return 80;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    SYDetailContentController *detail=[[SYDetailContentController alloc]init];
//    detail.newsModel=frameModel.newsModel;  //传递模型
    
    [self.navigationController pushViewController:detail animated:YES];
}

#pragma mark - 上拉
-(void)loadData
{
    _networkCount++;
    //判断有没有网络
    if([SYBaseMethod connectionInternet]==NO && _networkCount>1){
        [SYBaseMethod showErrorWithStr:@"网络断开了" toView:self.view];
        
    }
    
    NSString *url = [NSString stringWithFormat:@"/nc/article/%@/0-20.html",self.urlString];
    [self loadDataForType:1 withURL:url];
}

#pragma mark - 上拉
-(void)loaMoreData
{
    
    NSString *url = [NSString stringWithFormat:@"/nc/article/%@/%d-20.html",self.urlString,
                     (int)(self.arrayList.count - self.arrayList.count%10)];
    
//    NSLog(@"-->url--------》%@",url);
    [self loadDataForType:2 withURL:url];
    //判断有没有网络
    if([SYBaseMethod connectionInternet]==NO){
        [SYBaseMethod showErrorWithStr:@"网络断开了" toView:self.view];
        [self.tableView footerEndRefreshing];
    }
}

#pragma mark - 公共网络方法
- (void)loadDataForType:(int)type withURL:(NSString *)url
{
    //如果idStr有值得话，就是下拉请求新数据 就缓存，如果不是就不缓存。
    NSString *idStr=nil;
    if(type==1){
        idStr=[[self.urlString componentsSeparatedByString:@"/"] lastObject];
        
    }
    
    [SYHttpTool getWithUrl:url  parms:nil  idStr:idStr networkCount:self.networkCount  success:^(id json) {
        NSLog(@"-->getWithUrl--------》%@",url);
        NSLog(@"-->idStr--------》%@",idStr);
        NSLog(@"-->networkCount--------》%ld",(long)self.networkCount);
//        NSLog(@"-->json--------》%@",json);
        
        NSArray *arrModel=[SYNewsModel objectArrayWithKeyValuesArray:json];
        
        //下拉刷新的方法
        if(type==1){
            //清空模型里面的所有数据
            [self.arrayList removeAllObjects];
            //存放headerView数据的数组
            NSMutableArray *headerArr=[NSMutableArray arrayWithCapacity:4];
            
            for(SYNewsModel *newModel in arrModel){
                //1.不包括头部新闻的全部数据 (第一个数据是头数据)
                if(headerArr.count<=3){
                    [headerArr addObject:newModel];
                }
                //2.新闻列表的数据
                SYNewsFrameModel *frameModel=[[SYNewsFrameModel alloc]init];
                frameModel.newsModel=newModel;
                [self.arrayList addObject:frameModel];
                
            }
            
            //传递数据给headerView
//            self.headerV.arr=headerArr;
            
            [self.tableView headerEndRefreshing];
            [self.tableView reloadData];
            
        }else if(type==2){  //上啦刷新
            
            NSMutableArray *temp=[NSMutableArray array];
            for(SYNewsModel *newModel in arrModel){
                SYNewsFrameModel *frameModel=[[SYNewsFrameModel alloc]init];
                frameModel.newsModel=newModel;
                [temp addObject:frameModel];
            }
            [self.arrayList addObjectsFromArray:temp];
            
            [self.tableView footerEndRefreshing];
            [self.tableView reloadData];
        }
        
    } failture:^(id error) {
        [self.tableView headerEndRefreshing];
        [self.tableView footerEndRefreshing];
        NSLog(@"%@",error);
    }];
}

-(void)dealloc
{
    SYNSString(@"dealloc    ");
}
@end
